//
//  MPBankPhoneController.m
//  MarkPlatform
//
//  Created by xiao6 on 2018/3/9.
//  Copyright © 2018年 AT. All rights reserved.
//

#import "MPBankPhoneController.h"
#import "MPBankSmsCodeCell.h"
#import "MPAppButton.h"
#import "BaseWebController.h"
#import "MPBankBindSuccessController.h"

@interface MPBankPhoneController () {
    UITextField *_smsCodeTextField;
}
@property (nonatomic, strong) MPAppButton *bottomBtn;
@property (nonatomic, strong) MPBankModel *bankModel;

@end

@implementation MPBankPhoneController
+ (instancetype)vcWithModel:(MPBankModel *)bankModel {
    MPBankPhoneController *vc = [[self alloc] init];
    vc.bankModel = bankModel;
    return vc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"验证手机号" backItem:YES];
    [self setupViews];
}

#pragma mark - actions
- (void)getSmsCode:(MPBankSmsCodeCell *)cell {
    // 结束编辑
    [self.view endEditing:YES];
    // 获取验证码
    [MBProgressHUD showToView:self.view completion:^(MBProgressHUD *hud) {
        [MPHttpTool POST:[NSString stringWithFormat:@"business/getcode/%@", self.bankModel.tel]
                  params:nil
                 success:^(NSURLSessionDataTask *task, ATApiResult *result) {
                     [cell setGetSMSCodeDelay];
                     [hud hideWithMessage:@"验证码发送成功" completion:^{
                         [_smsCodeTextField becomeFirstResponder];
                     }];
                 } failure:^(NSString *errorMsg) {
                     [hud hideWithMessage:errorMsg completion:^{
                         [_smsCodeTextField  becomeFirstResponder];
                     }];
                 }];
    }];
}

- (void)didClickBottomBtn:(id)sender {
    // 结束编辑
    [self.view endEditing:YES];
    if (_smsCodeTextField.text.length < 4) {
        [MBProgressHUD showMessage:@"请输入正确的验证码"];
        [_smsCodeTextField becomeFirstResponder];
        return;
    }
    // 绑定
    [MBProgressHUD showWithCompletion:^(MBProgressHUD *hud) {
        [MPHttpTool POST:@"business/bank/verifyCode"
                  params:@{@"code" : SafeDicObj(_smsCodeTextField.text),
                           @"tel" : SafeDicObj(self.bankModel.tel),
                           }
                 success:^(NSURLSessionDataTask *task, ATApiResult *result) {
                     [MPHttpTool POST:@"business/wallet/bindBank"
                               params:@{@"userID" : SafeDicObj([ATLocal user].userId),
                                        @"name":SafeDicObj(self.bankModel.userName),
                                        @"card":SafeDicObj(self.bankModel.card),
                                        @"code":SafeDicObj(self.bankModel.code),
                                        @"idCard":SafeDicObj(self.bankModel.idCard),
                                        @"tel":SafeDicObj(self.bankModel.tel),}
                              success:^(NSURLSessionDataTask *task, ATApiResult *result) {
                                  [hud hideAnimated:YES];
                                  [self.navigationController pushViewController:[MPBankBindSuccessController new] animated:YES];
                              } failure:^(NSString *errorMsg) {
                                  [hud hideAnimated:YES];
                                  [ATAlertView showTitle:errorMsg message:nil normalButtons:nil highlightButtons:@[@"知道了"] completion:nil];
                              }];
                 } failure:ATHttpFailureWithHud(hud)];
    }];
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MPBankSmsCodeCell *cell = [MPBankSmsCodeCell cellForTableView:tableView indexPath:indexPath];
    cell.textFiled.text = _smsCodeTextField.text;
    _smsCodeTextField = cell.textFiled;
    [cell.textFiled notifyChange:^NSInteger(UITextField *textField, NSString *text) {
        return 4;
    }];
    __weak typeof(self) weakSelf = self;
    [cell setDidClickButton:^(MPBankSmsCodeCell *cell) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf getSmsCode:cell];
    }];
    return cell;
}

#pragma mark - setupView
- (void)setupViews {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 108)];
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16.0f];
    label.numberOfLines = 0;
    self.tableView.tableHeaderView = label;
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] init];
    [attr appendString:@"本次操作需要短信确认, 请输入\r\n" attribute:@{NSForegroundColorAttributeName : [UIColor colorWithRGB:0x999999]}];
    [attr appendString:self.bankModel.tel.EncTel attribute:@{NSForegroundColorAttributeName : [UIColor colorWithRGB:0x333333]}];
    [attr appendString:@" 收到的短信验证码" attribute:@{NSForegroundColorAttributeName : [UIColor colorWithRGB:0x999999]}];
    label.attributedText = attr;
    
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
    [footer addSubview:self.bottomBtn];
    [self.bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomBtn.superview.mas_left).offset(16);
        make.centerX.equalTo(self.bottomBtn.superview);
        make.top.equalTo(self.bottomBtn.superview).offset(60);
        make.height.equalTo(@(SCALEW(49.0f)));
    }];
    
    self.tableView.tableFooterView = footer;
}

#pragma mark - getter
- (UIButton *)bottomBtn {
    if (!_bottomBtn) {
        _bottomBtn = [MPAppButton buttonWithTitle:@"下一步" target:self action:@selector(didClickBottomBtn:)];
    }
    return _bottomBtn;
}
@end
